stopcam = false;
_Offset = [0,10,0];
private ["_source"];
vts3ddisplacement=false;
Zpos = 0;
VtsHight = 0.0;
VTS_Freecam_nvg = false;
VTS_Freecam_on = true;
VTS_Freecam_help = false;
VTS_Freecam_players = [];
_playableUnits = playableUnits;
VTS_Freecam_target = player;
//rabbit ! because we can attach stuff to it !
Obj = "Rabbit" createVehicle [spawn_x , spawn_y ,spawn_z] ;
Obj setVehicleInit "this hideObject true;";
processInitCommands;
 
[Obj] execVM "scripts\VTS_Functions\dontfall.sqf";
lapin = Obj;
	

ObjArrow = "Sign_arrow_down_large_EP1" createVehicleLocal [spawn_x, spawn_y ,spawn_z] ;
ObjArrow attachTo [Obj,[0,0,0]]; 

			
OBJdir = 0;

Obj allowDamage false;

sleep 0.25;

//Function
vts_3ddisplacement={
    switch (vts3ddisplacement) do
    {
    case false:
    {
      sleep 0.25;
      _list = nearestObjects  [[getPos lapin select 0,getPos lapin select 1,getPos lapin select 2],["Man","Car","Truck","Tank","Helicopter","Plane","Ship","ReammoBox","StaticWeapon"],500];
	    
	    _n=0;
	    _source = _list select _n;
	    while {_source==lapin} do
	    {
	    _source = _list select _n;
	    _n=_n+1;
      };
      
		  if ((count _list)==0) exitwith {breakclic = 0;hint "!!! No units found !!!"};
      
      object3D = _source;

			if (typeof object3D == "Man")  then { VtsHightstep = 1;VtsHight = 0; VtsHightmin = 0; VTScamtarget = lapin};
			if (typeof object3D == "ReammoBox")  then { VtsHightstep = 0.5;VtsHight = -0.5; VtsHightmin = -5};
	    object3D attachTo [lapin,[0,0,VtsHight]];
	    hint "Pickup";
      vts3ddisplacement=true;
    };
    case true:
    {
      sleep 0.25;
      hint "Drop";
      detach object3D;
      vts3ddisplacement=false;
    };
    };
};

//create camera
VTS_Freecam = "camconstruct" camcreate [spawn_x, spawn_y, 7];
VTS_Freecam camConstuctionSetParams [[spawn_x,spawn_y,spawn_z], 20000, 10000];
VTS_Freecam camSetTarget player;
VTS_Freecam cameraeffect ["internal","back"];

//Spacekey to exit
_keydown_space = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) == 57 ) then {
		stopcam = true;
	};
"];

_keydown_Backspace = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) == 14 ) then {
		stopcam = true;
	};
"];

//Displace the unit 

_keydown_c_key = (finddisplay 46) displayaddeventhandler ["keydown", "
  if ((_this select 1) == 46 )then  {
    _tmp = [] spawn vts_3ddisplacement;
  };
  "];

/*
_keydown_ShiftL = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) == 42 ) then {
		ShiftL = true;
		}
		else {
		ShiftL = false;
	};
"];
*/

_keydown_NightVision = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) in (actionkeys ""NightVision"")) then {
		if (VTS_Freecam_nvg) then {camUseNVG false;VTS_Freecam_nvg = false;}
		else {camUseNVG true;VTS_Freecam_nvg = true;};
	};
"];


_keydown_Help = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) in (actionkeys ""Help"")) then {
		if (not VTS_Freecam_help) then {
			Titletext  [""
                    Mousewheel:                                       Change height\n
                    Left mouse button:                                  Rotate left\n
                    Right mouse button:                                Rotate right\n
                    C:                                             Pick/Drop object\n
                    Space:                                                     Exit\n
                    Movement keys and mouse:                            Move camera\n
                                                                                   \n
                    Q:                                               Move camera up\n
                    Z:                                             Move camera down\n
                    SHIFT Left:                                       Slow movement\n
                                                                                   \n
                    Nightvision key:                             Nightvision On/Off\n
                                                                                   \n
                    Press the help button to exit the help screen."",""PLAIN""];
			VTS_Freecam_help = true;
		}
		else {
			Titletext ["""",""PLAIN""];
			VTS_Freecam_help = false;
		};
	};
"];

_keydown_Rclick = (finddisplay 46) displayaddeventhandler ["MouseButtonDown", "
	if ((_this select 1) == 0) then {
	OBJdir = getdir Obj;
	Obj setDir (OBJdir - 22.5);
};
"];


_keydown_Lclick = (finddisplay 46) displayaddeventhandler ["MouseButtonDown", "
	if ((_this select 1) == 1) then {
	OBJdir = getdir Obj;
	Obj setDir (OBJdir + 22.5);
};
"];

_mousez = (findDisplay 46) displayAddEventHandler ["mousezchanged", "
		_z = _this select 1;
		if (_z  < 0) then {
			Zpos = Zpos - 0.3;
		}
		else {
			Zpos = Zpos + 0.3;
		};
"];


//setpos loop 20 M before the camera

while {!stopcam} do {
sleep 0.01;
_Objpos = VTS_Freecam modelToWorld _Offset;
Obj setPos [_Objpos select 0,_Objpos select 1,Zpos];
};


//exit
//waitUntil {stopcam};
deleteVehicle Obj;
deleteVehicle ObjArrow;
VTS_Freecam_target switchCamera "INTERNAL";
VTS_Freecam CameraEffect ["Terminate","Back"];
CamDestroy VTS_Freecam;
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_Help];
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_NightVision];
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_space];
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_Backspace];
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_c_key];
(finddisplay 46) displayRemoveEventHandler ["mousezchanged",_mousez];
(finddisplay 46) displayRemoveEventHandler ["MouseButtonDown",_keydown_Rclick];
(finddisplay 46) displayRemoveEventHandler ["MouseButtonDown",_keydown_Lclick];

